<?php

error_reporting(E_ALL);

/**
 * cn.solor.dig - class.DiggerCacheMysql.php
 *
 * $Id: class.DiggerCacheMysql.php,v 1.2 2008-01-14 15:32:04 rainx Exp $
 *
 * This file is part of cn.solor.dig.
 *
 * Automatic generated with ArgoUML 0.22 on 17.12.2006, 10:07:15
 *
 * @author firstname and lastname of author, <author@example.org>
 */

if (0 > version_compare(PHP_VERSION, '5')) {
    die('This file was generated for PHP 5');
}

/**
 * include Digger
 *
 * @author firstname and lastname of author, <author@example.org>
 */
//require_once('interface.Digger.php');
//
////
//
//require_once('DigConfig.php');

/**
 * Short description of class DiggerCacheMysql
 *
 * @access public
 * @author firstname and lastname of author, <author@example.org>
 */
class DiggerCacheMysql
        implements Digger
{
    // --- ATTRIBUTES ---

    private $db;
    protected $title;

    // --- OPERATIONS ---
    /// build up connection
    function __construct()
    {
        $this->db = mysql_connect(DIGDBHOST, DIGDBUSER, DIGDBPASS);
        mysql_select_db(DIGDBNAME, $this->db);
    } 

    /**
     * Short description of method dig
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param string
     * @return array
     */
    public function dig($url)
    {
        $returnValue = array();
        $id = md5($url); // need not to escape it after it md5-ed sql
        $SQL = <<<SQL
        SELECT result,title from digCache where id='$id'
SQL;
        $result = mysql_query($SQL);
        if ($result)
        {
            if ($row = mysql_fetch_assoc($result))
            {
                $this->title = $row['title'];
                return unserialize($row['result']);
            }
        }
        return (array) $returnValue;
    }

    /**
     * Cache the result to mysql db
     */ 
    public function cache($url, &$resultRaw, $title ='', $cookie = '')
    {
        $id     = md5($url);
        $result = mysql_escape_string(serialize($resultRaw));
        $title  = mysql_escape_string($title); 
        $cookie = mysql_escape_string($cookie);
        $ip     = 0; //force to 0 first

        $SQL    = <<<SQL
        REPLACE INTO digCache set id = '$id',
                                  url = '$url',
                                  result = '$result',
                                  title  = '$title',
                                  cookie = '$cookie',
                                  ip     = '$ip'
SQL;
        mysql_query($SQL);
    }

    /**
     * Short description of method getTitle
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

} /* end of class DiggerCacheMysql */

?>
